//
//  WBStatusCell.swift
//  CustomWeiBo
//
//  Created by laiqiang on 21/5/28.
//  Copyright © 2021年 laiqiang. All rights reserved.
//

import UIKit
import SDWebImage

class WBStatusCell: UITableViewCell {

    func setupviewModel(viewModel: WBStatusViewModel?) {
        
        statusLable.text = viewModel?.status.text
        nameLable.text = viewModel?.status.user?.screen_name
        memberIconView.image = viewModel?.membericon
        iconView.wb_setimage(urlstring: viewModel?.status.user?.profile_image_url!, placeholderImage: UIImage(named: "mine_unactive"),isavatar: true)
        vipIconView?.image = viewModel?.vipimg
        
        toolbar.commentButton.setTitle(viewModel?.commentStr, for: [])
        toolbar.retweetedButton.setTitle(viewModel?.retweeteStr, for: [])
        toolbar.likesButton.setTitle(viewModel?.likeStr, for: [])
        
//        pictureView.heightCons.constant = 0
        
        pictureView.seupViewModel(viewmodel: viewModel)
        
        
        //设置成模型中计算好的高度
//        pictureView.heightCons.constant = viewModel?.pictureViewSize.height ?? 0
        
        
//        var urls = viewModel?.status.pic_urls ?? []
//        if urls.count > 4 {
//            urls.removeSubrange(urls.startIndex+4 ..< urls.endIndex)
//            pictureView.setupUrls(urls: urls)
//        }
//        else{
//            //设置url数据
//            pictureView.setupUrls(urls: viewModel?.status.pic_urls)
//        }
        
        //设置url数据
        pictureView.setupUrls(urls: viewModel?.picURLS)
        
        retweetedLabel?.text = viewModel?.retweetedText
    }
    
    //头像
    @IBOutlet weak var iconView: UIImageView!
    //姓名
    @IBOutlet weak var nameLable: UILabel!
    //会员图标
    @IBOutlet weak var memberIconView: UIImageView!
    //时间
    @IBOutlet weak var timeLable: UILabel!
    //来源
    @IBOutlet weak var sourceLable: UILabel!
    //认证
    @IBOutlet weak var vipIconView: UIImageView!
    //正文
    @IBOutlet weak var statusLable: UILabel!
    
    @IBOutlet weak var toolbar: WBStatusToolBar!
    
    @IBOutlet weak var pictureView: WBStatusPictureView!
    
    //一个文件绑定2个xib，原创cell没有这个属性，尾部只能用问号，不能！号
    @IBOutlet weak var retweetedLabel: UILabel?

    override func awakeFromNib() {
        super.awakeFromNib()

        //离屏渲染-异步绘制
        self.layer.drawsAsynchronously = true
        
        //珊格化-异步绘制后，生成一张独立的图像，cell在滚动时，就是滚动这张图片
        //cell优化要减少图层数量，这里就相当于只有一层
        //停止滚动后，可以接收监听
        self.layer.shouldRasterize = true
        
         //需要指定分辨率，否则会模糊
        self.layer.rasterizationScale = UIScreen.main.scale
        
        //离屏渲染，珊格化，这是高级优化，性能很好了就不要使用
        //因为离屏渲染需要cpu，gpu之间快速切换
        //√
        
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
